<?php
/**
 * Author: wl
 * Email: 446399160@qq.com
 * DateTime: 2017/05/01 13:31
 */

namespace app\modules\v1\models;
use yii\db\ActiveRecord;
use app\modules\v1\models\MerchantWeixin;
use yii\base\Exception;
use yii;

class ThreeElementsQueryLog extends ActiveRecord{
	
	public static function tableName()
	{
			return '{{three_elements_query_log}}';
	}
	
	public function init()
	{
			parent::init();
	}
	
	public static function MindateNow(){
		list($msec, $sec) = explode(' ', microtime());
		$msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
		return $msectime;
	}
	
	public function ThreeCurlPost($url,$data=''){
		$headers = array(
				"Content-type: application/x-www-form-urlencoded;charset=\"utf-8\"",
		);	
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		$info = curl_exec($ch);
		if (curl_errno($ch)) {
			echo 'Errno'.curl_error($ch);
		}
		curl_close($ch);
		return $info;
	}
	
	public function CheckThree($authName,$authId,$bankCode,$openid){
		if(empty($openid) || empty($bankCode) || empty($authId) || empty($authName)){
			$res['code'] = '10400';
			$res['msg']  = '身份证号、姓名、银行卡号不一致';
			return $res;
		}
		//用户微信数据
		$weixininfo = MerchantWeixin::findByOpenid($openid);
		if(empty($weixininfo)){
			$res['code'] = '10400';
			$res['msg']  = '微信号错误';
			return $res;
		}
		$threeelementsquerylog = static::findOne([
				'identity_card'=>$authId,
				'merchant_real_name'=>$authName,
				'bank_no'=>$bankCode
				]);
		if(!empty($threeelementsquerylog)){
			if($threeelementsquerylog->result == '一致'){
				$res['code'] = '10000';
				$res['msg']  = '一致';
				return $res;
			}else{
				$res['code'] = '10400';
				$res['msg']  = '身份证号、姓名、银行卡号不一致';
				return $res;
			}
		}
			
		try {
			$map['authName'] 		= $authName;
			$map['authId'] 			= strtoupper($authId) ;
			$map['bankCode'] 		= $bankCode;
			$map['merchantNo'] 		= Yii::$app->params['third_merchantno'];
			$map['requestTime'] 	= date('YmdHis');
			$map['requestOrderNo'] 	= ThreeElementsQueryLog::MindateNow();
			ksort($map);
			$string = '';
			foreach ($map as $k=>$v){
				$string .= "$k=$v".'&';
			}
			$string 			=  substr($string,0,strlen($string)-1);
			$map['requestSign'] = md5($string.Yii::$app->params['third_key']);
			$param_post_string  = http_build_query($map);
			$result  = $this->ThreeCurlPost(Yii::$app->params['third_url'], $param_post_string);
			$result  = json_decode($result);
			if(!empty($result) && isset($result->retCode)){
				Yii::info($result, 'threeElementsQueryLog');
				if($result->retCode == '000000'){//验证一致
					$threelog = new ThreeElementsQueryLog();
					$threelog->merchant_id 	 = $weixininfo->merchant_id;
					$threelog->identity_card = $authId;
					$threelog->merchant_real_name = $authName;
					$threelog->bank_no 		= $bankCode;
					$threelog->order_id 	= isset($result->apiResult->requestOrderNo)?$result->apiResult->requestOrderNo:'';
					$threelog->start_time 	= date("Y-m-d H:i:s",time()+8*3600);
					$threelog->result 		= '一致';
					$threelog->end_time 	= date("Y-m-d H:i:s",time()+8*3600);
					$threelog->save();
					$res['code'] = '10000';
					$res['msg']  = '一致';
					return $res;
				}else{ //验证不一致
					$threelog = new ThreeElementsQueryLog();
					$threelog->merchant_id 	 = $weixininfo->merchant_id;
					$threelog->identity_card = $authId;
					$threelog->merchant_real_name = $authName;
					$threelog->bank_no 		= $bankCode;
					$threelog->order_id 	= isset($result->apiResult->requestOrderNo)?$result->apiResult->requestOrderNo:'';
					$threelog->start_time 	= date("Y-m-d H:i:s",time()+8*3600);
					$threelog->result 		= $result->retMsg;
					$threelog->end_time 	= date("Y-m-d H:i:s",time()+8*3600);
					$threelog->save();
					$res['code'] = '10400';
					$res['msg']  = '身份证号、姓名、银行卡号不一致';
					return $res;
				}
			}else{
				$res['code'] = '10400';
				$res['msg']  = '身份证号、姓名、银行卡号不一致';
				return $res;
			}
		}catch (Exception $e) {
			$res['code'] = '10400';
			$res['msg']  = '身份证号、姓名、银行卡号不一致';
			return $res;
		}
	}
	
 	
}